
package com.example.paper.common.datam.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * reader doRead service基类.
 *
 * @author Long Fengfan, 2022年10月13日

 */
@DS("reader")
public class BaseReaderService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

    @Autowired
    private M m;

//    public abstract BaseMapper<T> getDao();


    /**
     * 返回数量
     */
    public long countByMapParam(Map<String, Object> params) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(params);
        return m.selectCount(queryWrapper);
    }

    /**
     * 分页查询
     */
    public List<T> getPageList(Map<String, Object> params, String orderByColumn, int segment, int pageSize) {
        Page<T> page = new Page<>(segment, pageSize);
        page.setSearchCount(false);
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(params).orderByAsc(orderByColumn);
        m.selectPage(page, queryWrapper);
        List<T> records = page.getRecords();
        return records;

    }
}
